//
//  CarInfoViewController.m
//  OneCreditTreasure
//
//  Created by apple on 16/8/30.
//
//

#import "CarInfoViewController.h"
#import "CarInfoTableViewCell.h"
#import "CarTypeInfoTableViewCell.h"
#import "CarDetailInfoTableViewCell.h"
#import "MDMyCarViewController.h"

@interface CarInfoViewController () <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
@property (nonatomic) UITableView *tableView;
@property (nonatomic) UIButton *commitBtn;

//输入信息的输入框
@property (nonatomic, strong) UITextField *carNameTF;
@property (nonatomic, strong) UITextField *carPlateNumTF;
@property (nonatomic, strong) UITextField *carFrameTF;
@property (nonatomic, strong) UITextField *carEngineTF;
@property (nonatomic, strong) UITextField *carListingTimeTF;
@property (nonatomic, strong) UITextField *driveDistanceTF;
@property (nonatomic, strong) UITextField *carPriceTF;
@end

@implementation CarInfoViewController
#pragma mark - UITextField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField == self.carNameTF) {
        [self.carPlateNumTF becomeFirstResponder];
    } else if (textField == self.carPlateNumTF) {
        [self.carFrameTF becomeFirstResponder];
    } else if (textField == self.carFrameTF) {
        [self.carEngineTF becomeFirstResponder];
    } else if (textField == self.carEngineTF) {
        [self.carListingTimeTF becomeFirstResponder];
    } else if (textField == self.carListingTimeTF) {
        [self.driveDistanceTF becomeFirstResponder];
    } else if (textField == self.driveDistanceTF) {
        [self.carPriceTF becomeFirstResponder];
    } else if (textField == self.carPriceTF) {
        [textField resignFirstResponder];
    }
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return self.isModify;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
}

#pragma mark - UITableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 8;
}

kRemobeCellSeparator
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 4) {
        CarTypeInfoTableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"carTypeInfoCell" forIndexPath:indexPath];
        Cell.nCarLB.text = @"新车";
        Cell.secondLB.text = @"二手车";
        switch (_model.carType) {
            case YDBCarTypeNewCar: {
                Cell.nCarBtn.selected = YES;
                break;
            }
            case YDBCarTypeSecondCar: {
                Cell.secondBtn.selected = YES;
                break;
            }
        }
        Cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return Cell;
    } else if (indexPath.row == 6 || indexPath.row == 7) {
        CarDetailInfoTableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"carDetailInfoCell" forIndexPath:indexPath];
        if (indexPath.row == 6) {
            Cell.leftLB.text = @"形式距离";
            Cell.unitLB.text = @"公里";
            Cell.infoTF.text = _model.driveDistance;
            self.driveDistanceTF = Cell.infoTF;
        }
        if (indexPath.row == 7) {
            Cell.leftLB.text = @"汽车价格";
            Cell.unitLB.text = @"万元";
            Cell.infoTF.text = _model.carPrice;
            self.carPriceTF = Cell.infoTF;
        }
        Cell.infoTF.delegate = self;
        Cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return Cell;
    } else {
        CarInfoTableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"carInfoCell" forIndexPath:indexPath];
        if (indexPath.row == 5) {
            Cell.leftLB.text = @"上牌时间";
            Cell.infoTF.text = _model.listingDate;
            self.carListingTimeTF = Cell.infoTF;
        } else if (indexPath.row == 3) {
            Cell.leftLB.text = @"发动机号";
            Cell.infoTF.text = _model.engineNum;
            self.carEngineTF = Cell.infoTF;
        } else if (indexPath.row == 2) {
            Cell.leftLB.text = @"车架号";
            Cell.infoTF.text = _model.carFrameNum;
            self.carFrameTF = Cell.infoTF;
        } else if (indexPath.row == 1) {
            Cell.leftLB.text = @"车牌号";
            Cell.infoTF.text = _model.plateNum;
            self.carPlateNumTF = Cell.infoTF;
        } else {
            Cell.leftLB.text = @"车型";
            Cell.infoTF.text = _model.carName;
            self.carNameTF = Cell.infoTF;
        }
        Cell.infoTF.delegate = self;
        Cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return Cell;
    }
}

#pragma mark - UITableView Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - 生命周期 Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (instancetype)init {
    if (self = [super init]) {
        self.modify = NO;
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"修改" style:UIBarButtonItemStylePlain target:self action:@selector(modifyCarInfo:)];
        self.title = @"车辆信息";
    }
    return self;
}

#pragma mark - 方法 methods
- (void)modifyCarInfo:(UIBarButtonItem *)sender {
    self.modify = !self.isModify;
    sender.title = (self.modify == YES ? @"取消" : @"修改");
    if (self.isModify) {
        [self.carNameTF becomeFirstResponder];
    } else {
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}

- (void)commitCarInfo:sender {
    self.model.carName = self.carNameTF.text;
    self.model.plateNum = self.carPlateNumTF.text;
    self.model.carFrameNum = self.carFrameTF.text;
    self.model.engineNum = self.carEngineTF.text;
    self.model.listingDate = self.carListingTimeTF.text;
    self.model.driveDistance = self.driveDistanceTF.text;
    self.model.carPrice = self.carPriceTF.text;
    [self.navigationController popViewControllerAnimated:YES];
    MDMyCarViewController *vc = (MDMyCarViewController *)self.navigationController.viewControllers.lastObject;
    if (![vc.carModels containsObject:self.model]) {
        [vc.carModels addObject:self.model];
    }
    [vc.tableView reloadData];
    [vc.view showWarning:@"提交成功"];
}

#pragma mark - 懒加载 Lazy Load
- (UITableView *)tableView {
    if(_tableView == nil) {
        _tableView = [[UITableView alloc] init];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(0);
            make.bottom.equalTo(self.commitBtn.mas_top).equalTo(0);
        }];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_tableView registerClass:[CarInfoTableViewCell class] forCellReuseIdentifier:@"carInfoCell"];
        [_tableView registerClass:[CarTypeInfoTableViewCell class] forCellReuseIdentifier:@"carTypeInfoCell"];
        [_tableView registerClass:[CarDetailInfoTableViewCell class] forCellReuseIdentifier:@"carDetailInfoCell"];
        
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (UIButton *)commitBtn {
    if(_commitBtn == nil) {
        _commitBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.view addSubview:_commitBtn];
        [_commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(0);
            make.height.equalTo(44);
        }];
        [_commitBtn setTitle:@"提交" forState:UIControlStateNormal];
        _commitBtn.backgroundColor = kRGBA(0, 0, 0, 0.8);
        [_commitBtn addTarget:self action:@selector(commitCarInfo:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _commitBtn;
}

- (myCarModel *)model {
    if(_model == nil) {
        _model = [[myCarModel alloc] init];
    }
    return _model;
}

@end
